/* MutableTaskList.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the MutableTaskList class, which extends a TaskList to allow it to
be modified instead of simply viewed and selected.
*/

#ifndef __MZ_MUTABLE_TASK_LIST_H__
#define __MZ_MUTABLE_TASK_LIST_H__

#include "TaskList.h"

#include <istream>
#include <ostream>
#include <string>

using std::istream;
using std::ostream;
using std::string;



class MutableTaskList : public TaskList {
public:
	// Get a non-const reference to the task in the given row.
	Task &operator[](int row);
	// Also make a const reference available (pulled up from TaskList, since
	// otherwise overloading does not work between classes).
	using TaskList::operator[];

	// Read and write the file.
	bool Load(istream &in);
	bool Save(ostream &out) const;

	// Copy and paste text.
	void Paste(const string &text, bool asChild = false);
	const string &Copy(bool withFlags = true) const;

	// Expand and collapse the selected rows.
	void Expand();
	void Collapse();
	void RecursiveExpand();
	void RecursiveCollapse();

	// Modify the selected rows.
	void Toggle(unsigned short flag);
	void RecursiveToggle(unsigned short flag);
	void InsertSibling();
	void InsertChild();
	void MoveUp();
	void MoveDown();
	void MoveIn();
	void MoveOut();
	void DeleteSelection();
	
	// Sort the siblings of the node in the given row.
	// If the root row is given, sort its children instead.
	void SortAlphabetical(bool reverse);
	void SortByDate(bool reverse);
	
	// Swap in a new list for this category.
	void SwapList(list<Task> &newList);
	

private:
	// Perform some procedure on all disjoint trees in the given range.
	// (That is, each task that is not a child of another task in the range).
	typedef bool (MutableTaskList::*MemFun)(iterator first, iterator last);
	template <MemFun fun>
	void ForEachTree();

	// Functions to be called by ForEachTree. These will only be called with
	// a pair of pointers representing a set of tasks that can be moved as a
	// single unit.
	bool MoveBlockUp(iterator first, iterator last);
	bool MoveBlockDown(iterator first, iterator last);
	bool MoveBlockIn(iterator first, iterator last);
	bool MoveBlockOut(iterator first, iterator last);

	template <bool reveal, bool recursive>
	bool ExpandBlock(iterator first, iterator last);

	bool DeleteBlock(iterator first, iterator last);
	
	// This must be private, because it is defined in the .cpp file where no
	// derived class will be able to access it.
	template <class Compare>
	void Sort(Compare compare);

	// Get an iterator with the given relationship to the current one.
	iterator Parent(iterator it);
	iterator PreviousSibling(iterator it);
	iterator NextSibling(iterator it);
	iterator TreeEnd(iterator it);

	// Get the iterator for the visible row with the given index.
	iterator RowIt(int row);
	const_iterator RowIt(int row) const;
	
	// Find all siblings of the task in the given row.
	typedef vector< pair<iterator, iterator> > TreeList;
	TreeList &FindSiblings();
	TreeList &FindTrees();


private:
	// This functor changes the depth of tasks.
	// This is a possible argument to for_each().
	template <int delta>
	class ChangeDepth {
	public:
		void operator()(Task &task) const { task.SetDepth(task.Depth() + delta); }
	};

	// This functor sets or clears the given flag of tasks.
	// This is a possible argument to for_each().
	class SetFlagOfAll {
		unsigned short flag;
		bool set;
	public:
		SetFlagOfAll(unsigned short flag, bool set) : flag(flag), set(set) {}
		void operator()(Task &task) const { task.SetFlag(flag, set); }
	};

	// This functor sets or clears the given flag of tasks that are revealed.
	// This is a possible argument to for_each().
	class SetFlagOfVisibles {
		unsigned short flag;
		bool set;
	public:
		SetFlagOfVisibles(unsigned short flag, bool set) : flag(flag), set(set) {}
		void operator()(Task &task) const { if(task.IsRevealed()) task.SetFlag(flag, set); }
	};

	// Alphabetic task comparison.
	// This is a possible argument to sort().
	class LessAlphabetical {
	public:
		LessAlphabetical(bool reverse = false) : reverse(reverse) {}
		
		bool operator()(TreeList::const_reference a, TreeList::const_reference b) const
		{ return a.first->CollatesBefore(*b.first) ^ reverse; }
		
	private:
		bool reverse;
	};
	
	// Task comparison by date.
	// This is a possible argument to sort().
	class LessByDate {
	public:
		LessByDate(bool reverse = false) : reverse(reverse) {}
		
		bool operator()(TreeList::const_reference a, TreeList::const_reference b) const
		{ return a.first->DateIsBefore(*b.first) ^ reverse; }
		
	private:
		bool reverse;
	};
};



#endif

/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
